package com.neuedu.my12306.web;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neuedu.my12306.entity.UserInfo;
import com.neuedu.my12306.service.IUserInfoService;
import com.neuedu.my12306.service.impl.IUserInfoServiceImpl;
import com.neuedu.my12306.util.DateUtil;

/**
 * Servlet implementation class RegServlet
 */
@WebServlet("/RegServlet.do")
public class RegServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IUserInfoService userInfoService=null;
	
	public RegServlet() {
		super();
		this.userInfoService = new IUserInfoServiceImpl();
	}   
   
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.在接收数据前进行编码转换(统一使用过滤器转换)
		//2.接收用户输入的参数
		String userName = request.getParameter("userName");
		String userPass = request.getParameter("userPass");
		String trueName = request.getParameter("trueName");
		String sex = request.getParameter("sex");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String cardType = request.getParameter("cardType");
		String idNumber = request.getParameter("idNumber");
		String passengerType = request.getParameter("passengerType");
		String tips = request.getParameter("tips");
		String birthDate = request.getParameter("birthDate");
		//对于多列数据有默认值的，可以在代码中进行硬编码
		String defaultHeadImage = "images/photo.jpg";
		int defaultUserPower = 0;
		Date birthDate2 = DateUtil.pareDate(birthDate);
		 
		UserInfo userInfo = new UserInfo(userName, userPass, trueName, sex, province, city, cardType, idNumber, passengerType, tips, birthDate2);
		userInfo.setHeadImage(defaultHeadImage);
		userInfo.setUserPower(defaultUserPower);
		boolean res = userInfoService.reg(userInfo);
		if (res) {
			response.sendRedirect("Login.jsp");
		}else {
			request.getRequestDispatcher("UserRegistration.html");
		}
	
	}



}
